﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.DeepZoomTools;

namespace SampleHelper
{
    public class DeepZoomHelper
    {
        public DeepZoomHelper()
        {
        }

        private string _destinationFolder;
        public string DestinationFolder
        {
            get { return _destinationFolder; }
            set { _destinationFolder = value; }
        }

        public void CreateCollection(List<string> definitionFiles, string outputfolder)
        {
            CollectionCreator cc = new CollectionCreator();
            //int tileSize = 256;
            //int maxLevel = Convert.ToInt32(Math.Log(tileSize, 2));

            //cc.TileSize = tileSize;
            cc.TileFormat = Microsoft.DeepZoomTools.ImageFormat.Png;
            //cc.MaxLevel = maxLevel;
            cc.ImageQuality = 1.00;
            cc.UseOptimizations = true;

            cc.Create(definitionFiles, outputfolder);
        }

        public string CreateSingleImage(
            string workpath,
            string collectionName,
            string image,
            string imageName,
            bool overrideExistingImages) // , Collection pc)
        {
            Console.WriteLine("Create Single Image: " + Path.GetFileNameWithoutExtension(image));
            ImageCreator ic = new ImageCreator();

            //ic.TileSize = 256;
            //ic.MaxLevel = Convert.ToInt32(Math.Log(ic.TileSize, 2));
            //ic.MaxLevel = pc.MaxZoomLevel;
            ic.TileFormat = Microsoft.DeepZoomTools.ImageFormat.Png;
            ic.ImageQuality = 1.00;
            ic.TileOverlap = 0;

            string filename = CreateCleanFilename(imageName);

            string target = workpath
                //+ "\\" 
                + collectionName
                + "_images\\"
                + filename;
            string defFile = Path.ChangeExtension(target, ".xml");

            if (!overrideExistingImages & (Directory.Exists(target + "_files") && File.Exists(defFile)))
            {
                Console.WriteLine("  - File already exist");
            }
            else
            {
                try
                {
                    ic.Create(image, target);
                }
                catch (Exception exc)
                {
                    Console.WriteLine("ERROR");
                    Console.WriteLine(image);
                    Console.WriteLine(exc.Message);
                    defFile = string.Empty;
                }
            }
            return defFile;
        }

        public static string CreateCleanFilename(string imageName)
        {
            string filename = imageName;
            filename = filename.Replace("\"", string.Empty);
            if (filename.IndexOf("<a name=") > 0)
            {
                int idx = filename.IndexOf("<a name=");
                filename = filename.Substring(0, idx);
            }
            filename = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(filename));
            return filename;
        }

    }
}

